﻿@model Bricks.Site.Models.ContractFilterView
@{
    ViewBag.Title = "StageScheduleList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}


<script type="text/javascript">
    $(function () {
        var userType = $("#hUserType").val();
        if (userType == "0") {
            var trCommom = document.getElementById('trCommon');
            var table = trCommom.parentNode;
            table.removeChild(trCommom);

            $("#YearRequest").combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=Year",
                onLoadSuccess: function () {
                    var data = $('#YearRequest').combobox('getData');
                    if (data.length > 0) {
                        $('#YearRequest').combobox('select', data[0]["DictionaryName"]);
                    }
                }
            });

            $("#MonthRequest").combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=Month",
                onLoadSuccess: function () {
                    var data = $('#MonthRequest').combobox('getData');
                    if (data.length > 0) {
                        $('#MonthRequest').combobox('select', data[0]["DictionaryName"]);
                    }
                }
            });
        }
        else {
            var trRequest = document.getElementById('trRequest');
            var table = trRequest.parentNode;
            table.removeChild(trRequest);


            $("#YearCommon").combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=Year",
                onLoadSuccess: function () {
                    var data = $('#YearCommon').combobox('getData');
                    if (data.length > 0) {
                        $('#YearCommon').combobox('select', data[0]["DictionaryName"]);
                    }
                }
            });

            $("#MonthCommon").combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=Month",
                onLoadSuccess: function () {
                    var data = $('#MonthCommon').combobox('getData');
                    if (data.length > 0) {
                        $('#MonthCommon').combobox('select', data[0]["DictionaryName"]);
                    }
                }
            });

            $('#ContractType').combobox({
                editable: false, panelHeight: "auto",
                valueField: "DictionaryName",
                textField: "DictionaryName",
                url: "/Dictionary/GetDictionaryByTypeTag?typeTag=ContractType",
                onLoadSuccess: function () {
                    var data = $('#ContractType').combobox('getData');
                    if (data.length > 0) {
                        $('#ContractType').combobox('select', data[0]["DictionaryName"]);
                    }

                    $("#ProjectID").combobox({
                        editable: false, panelHeight: "auto",
                        valueField: 'ID',
                        textField: 'ProjectName',
                        url: '/ProjectInfo/ProjectInfoListForFlow',
                        onLoadSuccess: function () {
                            var data = $("#ProjectID").combobox('getData');
                            if (data.length > 0) {
                                $("#ProjectID").combobox('select', data[0]["ID"]);
                            }
                        },
                        onSelect: function (record) {
                            var contractType = $("#ContractType").combobox('getValue');
                            initContract(record.ID, contractType);
                        }
                    });
                },
                onSelect: function (record) {
                    var projectID = $("#ProjectID").combobox('getValue');
                    initContract(projectID, record.DictionaryName);
                }
            });
        }
    });

    function initContract(projectID, contractType) {
        $("#ContractID").combobox({
            editable: false, panelHeight: "auto", multiple: true, separator: ',',
            valueField: 'ID',
            textField: 'ContractName',
            url: '/ContractInfo/ContractInfoListByProjectIDAndType?projectID=' + projectID + '&contractType=' + encodeURI(contractType),
            onLoadSuccess: function () {
                //var contractIDArr = new Array();
                var data = $("#ContractID").combobox('getData');
                if (data.length > 0) {
                    //for (var i = 0; i < data.length; i++) {
                    //    contractIDArr[i] = data[i]["ID"];
                    //}
                    $("#ContractID").combobox('select', data[0]["ID"]);
                }
                //$("#ContractID").combobox('setValues', contractIDArr);
            }
        });
    }

    function SearchRequest() {
        var projectID = $("#hProjectID").val();
        var contractType = encodeURI($("#ContractType").val());
        var contractID = $("#hContractID").val();
        var year = $("#YearRequest").combobox('getValue');
        var month = $("#MonthRequest").combobox('getValue');

        var url = '/StageSchedule/GetStageScheduleList?projectID=' + projectID + '&contractType=' + contractType + '&contractID=' + contractID + '&year=' + year + '&month=' + month;
        initTable(url);
    }

    function SearchCommon() {
        var projectID = $("#ProjectID").combobox('getValue');
        var contractType = $("#ContractType").combobox('getValue');
        var contractIDStr = $("#ContractID").combobox('getValues').toString();
        var year = $("#YearCommon").combobox('getValue');
        var month = $("#MonthCommon").combobox('getValue');
        var url = '/StageSchedule/GetStageScheduleListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&year=' + year + '&month=' + month;
        initTable(url);
    }

    function initTable(dataUrl) {
        $('#stageScheduleGrid').datagrid({
            url: dataUrl,
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            pagination: true,
            pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            toolbar: '#stageScheduleToolBar',
            columns:
            [
                [
                    { title: 'ID', field: 'ID', hidden: true },
                    { title: 'Year', field: 'Year', hidden: true },
                    { title: 'Month', field: 'Month', hidden: true },
                    { title: '统计周期', field: 'StatisticalCycle', align: 'center', rowspan: 2, width: 100 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 2, width: 50 },
                    { title: '总金额', field: 'TotalMoneyNum', align: 'center', rowspan: 2, width: 100 },
                    { title: '本月计划', field: 'CurrentMonthPlanMoneyNum', align: 'center', rowspan: 2, width: 100 },
                    { title: '阶段完成', field: '', colspan: 2, width: 200 },
                    { title: '开工累计完成', field: '', colspan: 2, width: 200 }
                ],
                [
                     { title: '金额', field: 'StageCompletedMoneyNum', align: 'center', width: 100 },
                     { title: '占月计划%', field: 'StageCompletedCurrentMonthPlanProportion', align: 'center', width: 100 },

                     { title: '金额', field: 'ConstructionCompletedMoneyNum', align: 'center', width: 100 },
                     { title: '占总金额%', field: 'ConstructionCompletedTotalProportion', align: 'center', width: 100 }
                ]
            ],
            onClickRow: function (rowIndex, rowData) {
                $('#btnDelete').linkbutton('enable');
                var url = '';
                var userType = $("#hUserType").val();
                if (userType == "0") {
                    url = '/StageSchedule/GetStageScheduleDetailList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val() + '&year=' + rowData.Year + '&month=' + rowData.Month + '&stageScheduleID=' + rowData.ID;
                }
                else {
                    var projectID = $("#ProjectID").combobox('getValue');
                    var contractType = $("#ContractType").combobox('getValue');
                    var contractIDStr = $("#ContractID").combobox('getValues').toString();
                    url = '/StageSchedule/GetStageScheduleDetailListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&year=' + rowData.Year + '&month=' + rowData.Month;
                }
                initDetailTable(url);
            },
            onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                    $('#stageScheduleGrid').datagrid('selectRow', 0);
                    var row = $("#stageScheduleGrid").datagrid("getSelected");
                    var userType = $("#hUserType").val();

                    var url = '';
                    if (userType == "0") {
                        url = '/StageSchedule/GetStageScheduleDetailList?projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val() + '&year=' + row.Year + '&month=' + row.Month + '&stageScheduleID=' + row.ID;
                    }
                    else {
                        var projectID = $("#ProjectID").combobox('getValue');
                        var contractType = $("#ContractType").combobox('getValue');
                        var contractIDStr = $("#ContractID").combobox('getValues').toString();
                        url = '/StageSchedule/GetStageScheduleDetailListForPreview?projectID=' + projectID + '&contractType=' + encodeURI(contractType) + '&contractIDStr=' + contractIDStr + '&year=' + row.Year + '&month=' + row.Month;
                    }
                    initDetailTable(url);
                }
                else {
                    initDetailTable();
                }
            }
        });
    }

    function initDetailTable(dataUrl) {
        $('#stageScheduleDetailGrid').datagrid({
            url: dataUrl,
            iconCls: 'icon-bricks',
            nowrap: true,//当数据长度超出列宽时将会自动截取
            autoRowHeight: false,
            striped: true,//设置为true将交替显示行背景
            collapsible: true,
            pagination: true,
            pageSize: 20,
            remoteSort: false,
            idField: 'ID',//表明该列是一个唯一列。
            singleSelect: true,
            fitColumns: true,
            columns:
            [
                [
                    { title: '父项目名称', field: 'ParentStatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '项目名称', field: 'StatisticsItemName', align: 'center', rowspan: 3, width: 100 },
                    { title: '单位', field: 'Unit', align: 'center', rowspan: 3, width: 50 },
                    { title: '总工程量', field: '', width: 100 },
                    { title: '本月计划', field: '', width: 100 },
                    { title: '阶段完成', field: '', colspan: 2, width: 200 },
                    { title: '开工累计完成', field: '', colspan: 2, width: 200 }

                ],
                [

                     { title: '金额', field: 'TotalAmount', align: 'center', rowspan: 2, width: 100 },
                     { title: '工程量', field: '', width: 100 },
                     { title: '工程量', field: '', width: 100 },
                     { title: '占月计划%', field: 'StageCompletedCurrentMonthPlanProportion', align: 'center', rowspan: 2, width: 100 },
                     { title: '工程量', field: '', width: 100 },
                     { title: '占总工程量%', field: 'ConstructionCompletedTotalProportion', align: 'center', rowspan: 2, width: 100 }
                ],
                [

                     { title: '金额', field: 'CurrentMonthPlanAmount', align: 'center', width: 100 },
                     { title: '金额', field: 'StageCompletedAmount', align: 'center', width: 100 },
                     { title: '金额', field: 'ConstructionCompletedAmount', align: 'center', width: 100 }
                ]
            ],
            onLoadSuccess: function (data) {
                if (data.rows.length > 1) {
                    mergeCellsByField("stageScheduleDetailGrid", "ParentStatisticsItemName,StatisticsItemName");
                }
                if (data.rows.length > 0) {
                    mergeColCells("stageScheduleDetailGrid");
                }
            }
        });
    }

    var StageScheduleList =
    {
        Add: function () {
            var year = $("#YearRequest").combobox('getValue');
            var month = $("#MonthRequest").combobox('getValue');
            if ($("#hProjectID").val() == '' || $("#ContractType").val() == '' || $("#hContractID").val() == '' || year == '' || month == '') {
                $.messager.alert('操作提示', "项目、合同类型、标段、年和月不能为空！", 'info');
            }
            else {
                $.post("/UserMenuAction/GetUserDepartmentForFlow?menuCode=StageScheduleList", function (userDepartment) {
                    if (userDepartment == "") {
                        showMyWindow("#myWindow", "选择机构", "/UserMenuAction/ShowOrgByUserForFlow?menuCode=StageScheduleList", 500, 280, true, false);
                    }
                    else if (userDepartment == "**********") {
                        $.messager.alert('操作提示', '当前用户不属于任何机构，请先设置所属机构！', 'info');
                    }
                    else if (userDepartment == "++++++++++") {
                        $.messager.alert('操作提示', '当前用户的机构不属于流程申请配置机构，不能新增！', 'info');
                    }
                    else {
                        showMyWindow("#myWindow", "", '/StageSchedule/StageSchedule?MenuCode=StageSchedule&sourceType=1&processType=&sourceID=&lastUpdateUserDepartment=' + userDepartment + '&projectID=' + $("#hProjectID").val() + '&contractType=' + encodeURI($("#ContractType").val()) + '&contractID=' + $("#hContractID").val()+ '&year=' + year + '&month=' + month, 1000, 500, true, true, true);
                    }
                });
            }
        },

        Delete: function () {
            var row = $("#stageScheduleGrid").datagrid("getSelected");
            $.messager.confirm('操作提示', "确认删除统计周期【" + row.StatisticalCycle + "】吗？", function (r) {
                if (r) {
                    $.post('/StageSchedule/Delete?stageScheduleID=' + row.ID, function (res) {
                        if (res == "Success") {
                            SearchRequest();
                            $.messager.alert('操作提示', '删除成功!', 'info');
                        }
                        else {
                            if (res == "") {
                                $.messager.alert('操作提示', '删除失败!请查看该数据与其他模块下的信息的关联，或联系管理员。', 'info');
                            }
                            else {
                                $.messager.alert('操作提示', res, 'info');
                            }
                        }
                    });
                }
            });
        }
    }

    function cancelPopWindow(handleState) {
        if (handleState == "0") {
            SearchRequest();
        }
        colseMyWindow("#myWindow");
    }

    function CloseWindow(data) {
        colseMyWindow("#myWindow");
        SearchRequest();
        $.EasyuiSlide(data);
    }
</script>

<div class="easyui-layout" style="height:740px;width:auto;">
    <input type="hidden" id="hProjectID" name="ProjectID" value="@Model.ProjectID" />
    <input type="hidden" id="hContractID" name="ContractID" value="@Model.ContractID" />
    <input type="hidden" id="hUserType" name="UserType" value="@Model.UserType" />
    <div data-options="region:'north'" style="height:75px;">
        <table style="margin-top: 5px">
            <tr id="trCommon">
                <td>
                    <table>
                        <tr>
                            <td style="width: 95px; text-align: center">项目名称</td>
                            <td>
                                <input id="ProjectID" name="ProjectID" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td style="width: 95px; text-align: center">合同类型</td>
                            <td>
                                <input id="ContractType" name="ContractType" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td style="width: 95px; text-align: center">标段名称</td>
                            <td>
                                <input id="ContractID" name="ContractID" class="easyui-combobox" style="width: 450px;" />
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 95px; text-align: center">年份</td>
                            <td>
                                <input id="YearCommon" name="YearCommon" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td style="width: 95px; text-align: center">月份</td>
                            <td>
                                <input id="MonthCommon" name="MonthCommon" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td colspan="2">
                                <a href="#" id="btnSearchCommon" class="easyui-linkbutton" data-options="plain:true" onclick="SearchCommon();">查看</a>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr id="trRequest">
                <td>
                    <table>
                        <tr>
                            <td style="width: 95px; text-align: center">项目名称</td>
                            <td>
                                <input name="ProjectName" id="ProjectName" class="easyui-textbox" data-options="required:true" value="@Model.ProjectName" style="width: 175px;" readonly />
                            </td>
                            <td style="width: 95px; text-align: center">合同类型</td>
                            <td>
                                <input name="ContractType" id="ContractType" class="easyui-textbox" data-options="required:true" value="@Model.ContractType" style="width: 175px;" readonly />
                            </td>
                            <td style="width: 95px; text-align: center">标段名称</td>
                            <td>
                                <input name="ContractName" id="ContractName" class="easyui-textbox" data-options="required:true" value="@Model.ContractName" style="width: 175px;" readonly />
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 95px; text-align: center">年份</td>
                            <td>
                                <input id="YearRequest" name="YearRequest" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td style="width: 95px; text-align: center">月份</td>
                            <td>
                                <input id="MonthRequest" name="MonthRequest" class="easyui-combobox" style="width: 180px;" />
                            </td>
                            <td colspan="2">
                                <a href="#" id="btnSearchRequest" class="easyui-linkbutton" data-options="plain:true" onclick="SearchRequest();">查看</a>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'center'">
        <div class="easyui-layout" fit="true">
            <div data-options="region:'north'" style="height:250px;">
                <table id="stageScheduleGrid" fit="true"></table>
                <div id="stageScheduleToolBar">
                    @Html.Raw(ViewBag.ToolBarInfo)
                </div>
            </div>
            <div data-options="region:'center'">
                <table id="stageScheduleDetailGrid" fit="true"></table>
            </div>
        </div>
    </div>
</div>
